package xyf.p04.tree;

import commons.TreeNode;

/**
 * ClassName: P0617_Merge
 * Description:
 * Author: xyf
 * Date: 9/7/22 9:42 PM
 * Version: 1.0
 **/
public class P0617_Merge
{
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2)
    {
        if (root1 == null && root2 == null)
        {
            return null;
        }
        TreeNode node = null;
        if (root1 == null)
        {
            node = new TreeNode(root2.val);
            node.left = root2.left;
            node.right = root2.right;
        }
        else if (root2 == null)
        {
            node = new TreeNode(root1.val);
            node.left = root1.left;
            node.right = root1.right;
        }
        else
        {
            node = new TreeNode(root1.val + root2.val);
            node.left = mergeTrees(root1.left, root2.left);
            node.right = mergeTrees(root1.right, root2.right);
        }
        return node;
    }
}
